

class MyPagenation:

    def xx(self):
        try:
            page_num = abs(int(request.GET.get('page', 1)))
            page_num = 1 if page_num <= 0 else page_num
        except Exception as e:
            page_num = 1
        per_page_num = 10  # 每页显示10条
        customer_count = models.Customer.objects.all().count()
        shang, yu = divmod(customer_count, per_page_num)

        if yu:
            page_num_count = shang + 1
        else:
            page_num_count = shang
        if page_num >= page_num_count:
            page_num = page_num_count
        # 页面生成页码的数量
        page_num_show = 5
        half_show = page_num_show // 2
        if page_num - half_show > 0:
            start_page_num = page_num - half_show
            end_page_num = page_num + half_show + 1
        else:
            start_page_num = 1
            end_page_num = 6
        if end_page_num >= page_num_count:
            end_page_num = page_num_count + 1
            start_page_num = end_page_num - page_num_show
        page_num_range = range(start_page_num, end_page_num)

        page_html = ''
        page_pre_html = f"""
           <nav aria-label="Page navigation">
               <ul class="pagination">
                   <li>
                       <a href="/customers/?page={page_num - 1}" aria-label="Previous">
                           <span aria-hidden="true">&laquo;</span>
                       </a>
                   </li>
           """
        page_html += page_pre_html
        for i in page_num_range:
            if page_num == i:
                page_html += f'<li class="active"><a href="/customers/?page={i}">{i}</a></li>'
            else:
                page_html += f'<li><a href="/customers/?page={i}">{i}</a></li>'
        page_next_html = f"""
                       <li>
                       <a href="/customers/?page={page_num + 1}" aria-label="Next">
                           <span aria-hidden="true">&raquo;</span>
                       </a>
                   </li>
               </ul>
           </nav>
           """
        page_html += page_next_html